﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace ShapeManager
{
    public class UCircle : DrawObject
    {
        private const string entryCenter = "Center";
        private const string entryRadius = "Radius";
        private PointF handlePoint;
        public virtual PointF Center {  get => new PointF(X + Region.Width / 2, Y + Region.Height / 2);  }
        public virtual float Radius => Math.Abs(Math.Min(_rectangleF.Width, _rectangleF.Height) / 2);
        public override int HandleCount => 1;
        public UCircle() : this(0, 0,10) { }
        public UCircle(int x, int y, int radius) : base()
        {
            X = x-radius;   
            Y = y-radius;
            Width  = radius * 2;
            Height = radius * 2;
            handlePoint = new PointF(Width, Height/2);
            Description = "ROI";
            Type = ShapeType.Circle;
            Initialize();
        }
        public override void Normalize()
        {
            _rectangleF = GetNormalizedRectangle(X,Y,Width,Height);
        }
        public override DrawObject Clone()
        {
            UCircle uCircle = new UCircle();
            uCircle.Region = this.Region;
            uCircle.ID = this.ID;
            Clone(uCircle);
            return uCircle;
        }
        public override void Draw(Graphics graphics)
        {
            using (Pen pen = new Pen(BorderColor, BorderWidth))
            {
                graphics.DrawEllipse(pen, URectangle.GetNormalizedRectangle(Region));
            }
            if (!IsInterest)
            {
                using (Brush brush = new SolidBrush(Color.FromArgb(Alpha, BorderColor)))
                {
                    graphics.FillEllipse(brush, URectangle.GetNormalizedRectangle(Region));
                }
            }
            if (IsInterest && IsSelected)
            {
                using (Brush brush = new SolidBrush(Color.FromArgb(Alpha, BorderColor)))
                {
                    graphics.FillEllipse(brush, URectangle.GetNormalizedRectangle(Region));
                }
            }
            DrawCenter(graphics);
            DrawDescription(graphics);
        }
        public override void Move(float deltaX, float deltaY)
        {
            _rectangleF.X += deltaX;
            _rectangleF.Y += deltaY;
        }
        public override void MoveHandleTo(PointF point, int handleNumber)
        {
            float left = Region.Left;
            float top = Region.Top;
            float right = Region.Right;
            float bottom = Region.Bottom;
            float addLength = point.X - right;
            switch (handleNumber)
            {
                case 4:
                    left = left - addLength;
                    top = top - addLength;
                    right = point.X;
                    bottom = bottom + addLength;
                    break;
            }
            SetCircle(left, top, right - left, bottom - top);
        }
        public override PointF GetHandle(int handleNumber)
        {
            RectangleF rectangle = _rectangleF;
            float x, y, xCenter, yCenter;
            xCenter = rectangle.X + rectangle.Width / 2;
            yCenter = rectangle.Y + rectangle.Height / 2;
            x = rectangle.X;
            y = rectangle.Y;
            if (handleNumber == 4)
            {
                x = rectangle.Right;
                y = yCenter;
            }
            return new PointF(x, y);
        }
        public override int HitTest(PointF point)
        {
            if (IsSelected)
            {
                if (GetHandleRectangle(4).Contains(point))
                    return 4;
            }
            if (PointInObject(point))
                return 0;
            return -1;
        }
        public override void DrawHandles(Graphics g)
        {
            if (!IsSelected) return;
            using (SolidBrush brush = new SolidBrush(Color.FromArgb(200, HandleColor)))
            {
                g.FillEllipse(brush, GetHandleRectangle(4));
            }
        }
        protected override bool PointInObject(PointF point)
        {
            return Region.Contains(point);
        }
        public override bool IntersectsWith(RectangleF rectangle)
        {
            return Region.IntersectsWith(rectangle);
        }
        public override Cursor GetHandleCursor(int handleNumber)
        {
            switch (handleNumber)
            {
                case 4:
                    return Cursors.SizeWE;
                default:
                    return Cursors.Default;
            }
        }
        protected void SetCircle(float x1, float y1, float width, float heigth)
        {
            X = x1;
            Y = y1;
            Width = width;
            Height = heigth;
        }
        protected void DrawDescription(Graphics graphics)
        {
            using (SolidBrush brush = new SolidBrush(Color.LightGreen))
            {
                Font descriptionFont = new Font("Arial", 9);
                string text = $"{Description}({Center})";
                SizeF maxTextSize = graphics.MeasureString(text, descriptionFont);
                float x = X + (Width  - maxTextSize.Width) / 2;
                float y = Y + (Height - maxTextSize.Height) / 2 - maxTextSize.Height;
                graphics.DrawString(text, descriptionFont, brush, x, y);
            }
        }
        public static RectangleF GetNormalizedRectangle(float x, float y,float width,float heigth)
        {
            float radius = Math.Max(width, heigth);
            return new RectangleF(x,y,radius,radius);
        }
    }
}
